/********************************************************************
 
 Copyright (C), 2012年 dhc. All rights reserved.
 
 Filename: 	    MailStafferList.m
 
 Author:        muzongcun
 
 Version:       1.0
 
 Description:   MailStafferList
 
 History:
 
 muzongcun    2012/3/10     1.0     Create
 
 *********************************************************************/

#import "MailStafferList.h"
#import "MailPersonInfoCell.h"
#import "XmlRpc.h"
#import "Type.h"
#import "Session.h"
#import "TaskManager.h"
@implementation MailStafferList
@synthesize     contentArray;
@synthesize     stafferDelegate;
@synthesize     listTV;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{
	if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) 
	{
	}
	return self;
}

- (void)viewDidLoad 
{

    
	listTV = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 44.0, 320.0, 480.0-20-44) style:UITableViewStylePlain];
	listTV.delegate = self;
	listTV.dataSource = self;
	
	sBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
	sBar.delegate = self;
	sBar.autocorrectionType = UITextAutocorrectionTypeNo;
	sBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
	//listTV.tableHeaderView = sBar;
	
	moreBtn = [UIButton	buttonWithType:UIButtonTypeCustom];
	[moreBtn setTitle:@"" forState:UIControlStateNormal];
	[moreBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
	[moreBtn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
	[moreBtn addTarget:self action:@selector(showMoreContact:) forControlEvents:UIControlEventTouchUpInside];
	moreBtn.frame = CGRectMake(0.0, 0.0, 320.0, 44.0);
	//listTV.tableFooterView = moreBtn;

    self.navigationController.navigationBar.tintColor = NAVIGATION_BAR_COLOR;
    
	[self.view addSubview:listTV];
    //从服务器取得朋友列表
    //contentArray = [self readStafferList];
    //从服务器取得数据
    [self refreshData];
}

- (void)viewDidUnload 
{
    [super viewDidUnload];
    
}

- (void)dealloc 
{
	[listTV release];
	[sBar release];
    [super dealloc];
}

#pragma mark -
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
	
}

#pragma mark -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    NSInteger num = [contentArray count];
    return num;
}

//******************************************************
// Method:     tableView
// Access:     public 
// Returns:    void
// Parameter:  cellForRowAtIndexPath
// Note:	  
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
	static NSString *CellIdentifier = @"MailPersonInfoCell";

	MailPersonInfoCell *cell = (MailPersonInfoCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) 
    {
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"MailPersonInfoCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        //cell.accessibilityIdentifier = CellIdentifier;
	}
    NSLog(@"%@",contentArray);
    
    if (indexPath.row >= [contentArray count]) 
    {
        return cell;
    }
	NSString *name = [[contentArray objectAtIndex:indexPath.row] objectForKey:@"user_name_cn"];
    NSString *address = [[contentArray objectAtIndex:indexPath.row] objectForKey:@"mail_address"];
#ifdef _NO_USE    
    //通过url取得图片信息
	UIImage *image =[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: [[contentArray objectAtIndex:indexPath.row] objectForKey:@"url"]]]];
#endif 
    UIImage *imagePersonInfo =[UIImage  imageNamed:PIC_MAIL_PERSON_IMAGE];
	cell.personName.text    = name ;
    cell.personAddress.text = address ;
	[cell.imageView sizeToFit];
	[cell.imageView setImage:imagePersonInfo];
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
	[sBar resignFirstResponder];
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row >= [contentArray count]) 
    {
        return;
    }
	[stafferDelegate SelectStafferName:[contentArray objectAtIndex:indexPath.row]];
}

#pragma mark -
//******************************************************
// Method:     readFriendList
// Access:     public 
// Returns:    void
// Parameter:  void
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(NSMutableArray*)readStafferList
{
    NSString* strPosID = [[Session share] getMemberPosiID];
    XmlRpc *xml = [[XmlRpc alloc] init];
    NSMutableArray *stafferArray =  [[NSMutableArray alloc] initWithArray: [xml getEmployeeList:strPosID]]; 
    [xml release];
	return stafferArray;
}

//******************************************************
// Method:     showMoreContact
// Access:     public 
// Returns:    void
// Parameter:  senders
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(void)showMoreContact:(id)sender
{
	NSLog(@"more");
}

//******************************************************
// Method:     cancelSelected
// Access:     public 
// Returns:    void
// Parameter:  void
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(IBAction)cancelSelected:(id)sender
{
	[self dismissModalViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark UserTaskDelegate Methods

-(void) refreshData
{
    [[[TaskManager Share] GetTaskByType:mail_GetCustomerList Delegate:self] Excute];
}
- (void) OnPreExcute 
{

}

- (void) DoInBackground
{
    contentArray = [self readStafferList];
    [self.listTV reloadData];
}

- (void) OnPostExcute
{

}

@end
